package com.lorizz.docrepo.repository.impl;

import javax.jcr.LoginException;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import com.lorizz.docrepo.DocrepoException;
import com.lorizz.docrepo.repository.DocumentRepository;
import com.lorizz.docrepo.repository.DocumentRepositoryFactory;

public class JcrDocumentRepositoryFactory implements DocumentRepositoryFactory {

    private Repository repository;

    public void setRepository(Repository repository) {
        this.repository = repository;
    }

    @Override
    public DocumentRepository connect(String username, String password) {
        try {
            Session session = repository.login(new SimpleCredentials(username, password.toCharArray()));
            return new JcrDocumentRepository(session);
        } catch (LoginException e) {
            throw new DocrepoException(e);
        } catch (RepositoryException e) {
            throw new DocrepoException(e);
        }
    }
}
